package edu.cicese.alz.swing;

import sun.swing.FilePane;

import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 4/06/12
 * Time: 04:10 PM
 */
public class JSystemFileChooser extends JFileChooser {

//	private int maxDirs = Short.MAX_VALUE;

	/*public JSystemFileChooser() {
		super();
	}*/

	/*public JSystemFileChooser(int maxDirs) {
		super();
		this.maxDirs = maxDirs;
	}*/

	/*public void approveSelection() {
			if (getSelectedFiles().length > maxDirs) {
				JOptionPane.showMessageDialog(this, "Please select a maximum of " + maxDirs + " source directories.",
						"Error", JOptionPane.ERROR_MESSAGE,
						new ImageIcon(getClass().getClassLoader().getResource("icons/error.png")));
			}
			else {
				super.approveSelection();
			}
		}*/

	public void updateUI() {
//		LookAndFeel old = UIManager.getLookAndFeel();
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Throwable ex) {
//			old = null;
		}

		super.updateUI();
		paintPanes(this);
		setBackground(SwingUtil.cBackground);

		/*if (old != null) {
			FilePane filePane = findFilePane(this);
			filePane.setViewType(FilePane.VIEWTYPE_DETAILS);
			filePane.setViewType(FilePane.VIEWTYPE_LIST);

			Color background = UIManager.getColor("Label.background");
			setBackground(background);
			setOpaque(true);

			try {
				UIManager.setLookAndFeel(old);
			} catch (UnsupportedLookAndFeelException ignored) {
			} // shouldn't get here
		}*/


	}


	/*private FilePane findFilePane(Container parent) {
		for (Component comp : parent.getComponents()) {
			if (FilePane.class.isInstance(comp)) {
				return (FilePane) comp;
			}
			if (comp instanceof Container) {
				Container cont = (Container) comp;
				if (cont.getComponentCount() > 0) {
					FilePane found = findFilePane(cont);
					if (found != null) {
						return found;
					}
				}
			}
		}

		return null;
	}*/

	private void paintPanes(Container parent) {
		for (Component comp : parent.getComponents()) {
			if (FilePane.class.isInstance(comp)) {
				FilePane filePane = (FilePane) comp;
				filePane.setViewType(FilePane.VIEWTYPE_DETAILS);
				filePane.setViewType(FilePane.VIEWTYPE_LIST);
				setBackground(Color.WHITE);
				setOpaque(true);
			}
			else {
				if (comp instanceof JTextField) {
					((JTextField) comp).setEditable(false);
				}
				else if (comp instanceof Container) {
					Container cont = (Container) comp;
					if (cont.getComponentCount() > 0) {
						paintPanes(cont);
					}
					comp.setBackground(SwingUtil.cBackground);
				}
			}
		}
	}
}
